<template>
  <div class="app-container">
    <h2 style="text-align: center;">发布新课程</h2>
    <el-steps :active="2" process-status="wait" align-center style="margin-bottom: 40px;">
      <el-step title="填写课程基本信息"/>
      <el-step title="创建课程大纲"/>
      <el-step title="提交审核"/>
    </el-steps>
    <el-button type="text" @click="openAddChapter">添加章节</el-button>
    <ul class="chanpterList">
      <li v-for="chapter in chapterNestedList" :key="chapter.id">
        <p>{{chapter.title}}
          <span class="acts">
                <el-button type="text" @click="openAddVideo(chapter.id)">添加小节</el-button>
                <el-button style="" type="text" @click="editChapter(chapter.id)">编辑</el-button>
                <el-button type="text" @click="removeChapter(chapter.id)">删除</el-button>
            </span>
        </p>
        <ul class="chanpterList videoList">
          <li v-for="video in chapter.children" :key="video.id">
            <p>{{video.title}}
              <span class="acts">
                <el-button style="" type="text" @click="editVideo(video.id)">编辑</el-button>
                <el-button type="text" @click="removeVideo(video.id)">删除</el-button>
              </span>
            </p>
          </li>
        </ul>
      </li>
    </ul>

    <div>
      <el-button @click="previous">上一步</el-button>
      <el-button :disabled="saveBtnDisabled" type="primary" @click="next">下一步</el-button>
      <el-button :disabled="false" type="primary" @click="saveAndGoToCourseList">返回课程列表</el-button>
    </div>


    <!-- 添加和修改章节表单 -->
    <el-dialog :visible.sync="dialogChapterFormVisible" title="章节信息">
      <el-form :model="chapter" label-width="120px">
        <el-form-item label="章节标题">
          <el-input v-model="chapter.title"/>
        </el-form-item>
        <el-form-item label="章节排序">
          <el-input-number v-model="chapter.sort" :min="0" controls-position="right"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogChapterFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveOrUpdate">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogVidoFormVisible" title="小节信息">
      <el-form :model="video" label-width="120px">
        <el-form-item label="小节标题">
          <el-input v-model="video.title"/>
        </el-form-item>
        <el-form-item label="小节排序">
          <el-input-number v-model="video.sort" :min="0" controls-position="right"/>
        </el-form-item>
        <el-form-item label="是否免费">
          <el-switch
            v-model="video.isFree"
            active-color="#13ce66"
            inactive-color="#ff4949">
          </el-switch>
        </el-form-item>
        <el-form-item label="上传视频">
          <el-upload
            :on-success="handleVodUploadSuccess"
            :on-remove="handleVodRemove"
            :before-remove="beforeVodRemove"
            :on-exceed="handleUploadExceed"
            :file-list="fileList"
            :action="BASE_API+'/videoservice/vod/upload'"
            :limit="1"
            class="upload-demo">
            <el-button size="small" type="primary">上传视频</el-button>
            <el-tooltip placement="right-end">
              <div slot="content">最大支持1G，<br>
                支持3GP、ASF、AVI、DAT、DV、FLV、F4V、<br>
                GIF、M2T、M4V、MJ2、MJPEG、MKV、MOV、MP4、<br>
                MPE、MPG、MPEG、MTS、OGG、QT、RM、RMVB、<br>
                SWF、TS、VOB、WMV、WEBM 等视频格式上传</div>
              <i class="el-icon-question"/>
            </el-tooltip>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVidoFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveOrUpdateVideo">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
    import chapter from '@/api/chapter'
    import video from '@/api/video'
    import vod from '@/api/vod'

    export default {
        data() {
            return {
                saveBtnDisabled: false,
                courseId: '',         // 所属课程
                chapterNestedList: [], // 章节嵌套视频列表
                dialogChapterFormVisible: false, //是否显示章节表单
                dialogVidoFormVisible: false, //是否显示小节表单
                BASE_API: process.env.BASE_API, // 接口API地址
                fileList: [],//上传文件列表
                video: {
                    title: '',
                    sort: 0
                },
                chapter: {
                    title: '',
                    sort: 0,
                    courseId: ''
                }
            }
        },
        created() {
            this.init()
        },
        methods: {
            /*初始化课程大纲信息*/
            init() {
                this.courseId = this.$route.params.id
                this.getChapterVideoList()
            },
            previous() {
                this.$router.push({path: '/edu/course/info/' + this.$route.params.id})
            },

            next() {
                this.$router.push({path: '/edu/course/publish/' + this.$route.params.id})
            },
            /*保存并返回课程列表*/
            saveAndGoToCourseList() {
                this.$router.push({path: '/edu/course/list/'})
            },
            /*获取课程大纲*/
            getChapterVideoList() {
                chapter.getChapterVideoList(this.courseId)
                    .then(response => {
                        if (response.data.success) {
                            this.chapterNestedList = response.data.data.items
                        }
                    })

            },
            /*修改或者保存*/
            saveOrUpdate() {
                if (!this.chapter.id) {
                    this.saveChapter()
                } else {
                    this.updateChapter()
                }
            },

            /*保存章节*/
            saveChapter() {
                this.chapter.courseId = this.courseId
                chapter.saveChapter(this.chapter)
                    .then(response => {
                        this.dialogChapterFormVisible = false
                        this.chapter = {sort: 0}
                        if (response.data.success) {
                            this.$message({
                                type: 'success',
                                message: '添加章节成功'
                            })
                            //刷新页面
                            this.getChapterVideoList()
                        } else {
                            this.$message({
                                type: 'error',
                                message: '添加章节失败'
                            })
                        }
                    })
                    .catch(response => {
                        this.$message({
                            type: 'error',
                            message: '添加章节失败'
                        })
                    })
            },
            /*更新章节*/
            updateChapter() {
                chapter.updateChapter(this.chapter)
                    .then(response => {
                        this.dialogChapterFormVisible = false
                        this.chapter = {sort: 0}
                        if (response.data.success) {
                            this.$message({
                                type: 'success',
                                message: '修改章节成功'
                            })
                            this.getChapterVideoList()
                        } else {
                            this.$message({
                                type: 'error',
                                message: '修改章节失败'
                            })
                        }
                    })
                    .catch(response => {
                        this.$message({
                            type: 'error',
                            message: '修改章节失败'
                        })
                    })
            },
            /*编辑章节*/
            editChapter(id) {
                this.dialogChapterFormVisible = true
                this.getChapterById(id)
            },
            openAddChapter() {
                this.dialogChapterFormVisible = true
                this.chapter = {sort: 0}
            },
            /*删除章节*/
            removeChapter(id) {
                this.$confirm('此操作将永久删除该章节, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    return chapter.deleteChapterById(id)
                }).then((response) => {
                    this.getChapterVideoList()
                    if (response.data.success) {
                        this.$message({
                            type: 'success',
                            message: '删除章节成功'
                        })
                    } else {
                        this.$message({
                            type: 'error',
                            message: `${response.data.message}`
                        })
                    }
                }).catch((response) => { // 失败
                    if (response === 'cancel') {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        })
                    } else {
                        this.$message({
                            type: 'error',
                            message: response.message
                        })
                    }
                })
            },
            /*根据章节ID查找章节*/
            getChapterById(id) {
                chapter.getChapterInfo(id)
                    .then(response => {
                        if (response.data.success) {
                            this.chapter = response.data.data.chapter
                            console.log(this.chapter)
                        }
                    })
            },
            /*保存或者修改小节*/
            saveOrUpdateVideo() {
                if (!this.video.id) {
                    this.addVideo()
                }else {
                    //修改
                    this.updateVideo()
                }
            },
            openAddVideo(chapterId) {
                this.video = {sort: 0}
                this.dialogVidoFormVisible = true
                this.video.chapterId = chapterId
                this.fileList = []
            },
            /*保存小节*/
            addVideo() {
                this.video.courseId = this.courseId
                video.saveVideo(this.video)
                    .then(response => {
                        this.dialogVidoFormVisible = false
                        this.video = {sort: 0}
                        if (response.data.success) {
                            this.$message({
                                type: 'success',
                                message: '新增小节成功'
                            })
                            this.getChapterVideoList()
                        }else {
                            this.$message({
                                type: 'error',
                                message: '新增小节失败'
                            })
                        }
                    })
                    .catch(response => {
                        this.$message({
                            type: 'error',
                            message: '新增小节失败'
                        })
                    })
            },
            /*编辑小节*/
            editVideo(videoId) {
                this.dialogVidoFormVisible = true
                this.getVideoById(videoId)
            },
            /*根据小节ID查找小节*/
            getVideoById(videoId) {
                video.getVedioInfo(videoId)
                    .then(response => {
                        this.video = response.data.data.video
                        if (this.video.videoSourceId) {
                            this.fileList = [{'name':this.video.videoOriginalName}]
                        }else {
                            this.fileList = []
                        }
                    })
            },
            /*给更新小节*/
            updateVideo() {
                video.updateVideo(this.video)
                    .then(response => {
                        this.dialogVidoFormVisible = false
                        this.video = {sort: 0}
                        if (response.data.success) {
                            this.$message({
                                type: 'success',
                                message: '修改小节成功'
                            })
                            this.getChapterVideoList()
                        }else {
                            this.$message({
                                type: 'error',
                                message: '修改小节成功'
                            })
                        }
                    })
                    .catch(response => {
                        this.$message({
                            type: 'success',
                            message: '修改小节成功'
                        })
                    })
            },
            /*删除小节*/
            removeVideo(id) {
                this.$confirm('此操作将永久删除该小节, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    return video.deleteVideo(id)
                }).then((response) => {
                    this.getChapterVideoList()
                    if (response.data.success) {
                        this.$message({
                            type: 'success',
                            message: '删除小节成功'
                        })
                    } else {
                        this.$message({
                            type: 'error',
                            message: '删除小节失败'
                        })
                    }
                }).catch((response) => { // 失败
                    if (response === 'cancel') {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        })
                    } else {
                        this.$message({
                            type: 'error',
                            message: '删除小节失败'
                        })
                    }
                })
            },

            /*视频上传成功回调*/
            handleVodUploadSuccess(response, file, fileList) {
                this.video.videoSourceId = response.data.videoId
                this.video.videoOriginalName = file.name
            },

            //视图上传多于一个视频
            handleUploadExceed(files, fileList) {
              this.$message.warning('想要重新上传视频，请先删除已上传的视频')
            },
            /*删除之前提示框*/
            beforeVodRemove(file, fileList) {
                return this.$confirm(`确定移除 ${file.name}？`)
            },
            /*删除云端视频*/
            handleVodRemove(file, fileList) {
              vod.removeVideo(this.video.videoSourceId)
                  .then(response => {
                      if (response.data.success) {
                          this.video.videoSourceId = ''
                          this.video.videoOriginalName = ''
                          this.fileList = []
                          if (this.video.id) {
                              //编辑状态
                              video.updateVideo(this.video)
                          }
                          this.$message({
                              type: 'success',
                              message: '删除视频成功'
                          })
                      }else {
                          this.$message({
                              type: 'error',
                              message: '删除视频失败'
                          })
                      }
                  })
            }
        }
    }
</script>

<style scoped>
  .chanpterList {
    position: relative;
    list-style: none;
    margin: 0;
    padding: 0;
  }

  .chanpterList li {
    position: relative;
  }

  .chanpterList p {
    float: left;
    font-size: 20px;
    font-family: KaiTi;
    margin: 10px 0;
    padding: 10px;
    height: 70px;
    line-height: 50px;
    width: 100%;
    border: 1px solid #DDD;
  }

  .chanpterList .acts {
    float: right;
    font-size: 14px;
  }

  .videoList {
    padding-left: 50px;
  }

  .videoList p {
    float: left;
    font-size: 14px;
    margin: 10px 0;
    padding: 10px;
    height: 50px;
    line-height: 30px;
    width: 100%;
    border: 1px dotted #DDD;
  }
</style>
